<template>
  <div class="main_box_Freeback">
    <el-dialog v-model="dialogFormVisible" title="商店审核" width="500">
      <el-form-item label="审核状态" label-width="100">
        <el-select-v2
          v-model="freeback.state"
          :options="options"
          placeholder="Please select"
          size="large"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="手机号" label-width="100">
        {{ shopEInfo.phone }}
      </el-form-item>
      <el-form-item label="身份证照片" label-width="100">
        <el-image style="width: 100px" :src="shopEInfo.iccardImg" fit="cover" />
      </el-form-item>

      <el-form-item label="营业执照" label-width="100">
        <el-image style="width: 100px" :src="shopEInfo.businessLicense" fit="cover" />
      </el-form-item>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">Cancel</el-button>
          <el-button type="primary" @click="FreeBack"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>

    <el-table :data="tableData" height="70vh" style="width: 100%">
      <el-table-column prop="shopName" label="商店名称" width="180" />
      <el-table-column prop="shopMasterId.name" label="商店店主" width="180" />
      <el-table-column prop="shopIntroduce" label="商店介绍" />
      <el-table-column label="商店状态">
        <template #default="scope">
          {{
            scope.row.shopState == 0
              ? "未审核"
              : scope.row.shopState == 1
              ? "待审核"
              : scope.row.shopState == 2
              ? "审核未通过"
              : scope.row.shopState == 3
              ? "审核通过"
              : "error"
          }}
        </template>
      </el-table-column>
      <el-table-column label="商家图像">
        <template #default="scope">
          <img :src="scope.row.shopImg" style="height: 50px" />
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="创建时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button @click="handleEdit(scope.row.shopId, scope.row.shopState)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="page"
        v-model:page-size="pageItem"
        :page-sizes="[20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotal"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { Search } from "@element-plus/icons-vue";
import axios from "axios";
import { ElMessage } from "element-plus";
const pageTotal = ref(0);
const pageItem = ref(20);
const page = ref(1);
const tableData = ref([]);
const state = ref(1);
const search = ref("");
// do not use same name with ref
const content = ref("");
const dialogFormVisible = ref(false);

const freeback = reactive({
  id: "",
  state: "",
});
const shopEInfo = reactive({
  iccardImg: "",
  businessLicense: "",
  phone: "",
});

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  pageItem.value = val;
  getData();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  page.value = val;
  getData();
};
const token = localStorage.getItem("order_auth_token");

const FreeBack = () => {
  axios
    .get("/api/shop/update/state/" + freeback.id + "/" + freeback.state, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      console.log(res);
      if (res.data.success) {
        ElMessage({
          type: "success",
          message: "操作成功",
        });
      } else {
        ElMessage({
          type: "warning",
          message: res.data.message,
        });
      }
      getData();
    })
    .catch((err) => {
      console.log(err);
    });

  dialogFormVisible.value = false;
};
const handleEdit = (index: string, state: string) => {
  axios
    .get("/api/examineShop/get/" + index, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log("code line-150 \n\r😒 res:\n\r", res);
      if (res.data.success) {
        const msg = res.data.message;
        shopEInfo.iccardImg = msg.idcardImg;
        shopEInfo.businessLicense = msg.businessLicense;
        shopEInfo.phone = msg.phone;
      } else {
        ElMessage({
          type: "error",
          message: res.data.message,
        });
        shopEInfo.iccardImg = "";
        shopEInfo.businessLicense = "";
        shopEInfo.phone = "";
      }
    })
    .catch((err) => {});
  freeback.id = index;
  freeback.state = state;
  dialogFormVisible.value = true;
  console.log(index);
};
const options = [
  {
    label: "没有审核",
    value: 0,
  },
  {
    label: "正在审核",
    value: 1,
  },
  {
    label: "审核不通过",
    value: 2,
  },
  {
    label: "审核通过",
    value: 3,
  },
];
const getData = () => {
  const token = localStorage.getItem("order_auth_token");
  axios
    .get("/api/shop/query/school/" + page.value + "/" + pageItem.value + "?shopName=", {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log(res);
      tableData.value = res.data.message.list;
      tableData.value.forEach((el) => {
        el.shopMasterId = JSON.parse(el.shopMasterId);
      });
      pageTotal.value = res.data.message.total;
      console.log(res.data.message.list);
    })
    .catch((err) => {
      console.log(err);
    });
};
getData();
</script>

<style lang="scss" scoped>
.main_box_Freeback {
  height: 100%;
  width: 100%;
}
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
